package com.anpy.management.controller;

import com.anpy.common.service.BaseController;
import com.anpy.common.service.model.vo.ResponseMessage;
import com.anpy.common.service.model.vo.Result;
import com.anpy.management.entity.ProjectEntity;
import com.anpy.management.service.ProjectService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 项目管理控制器
 */
@RestController
@RequestMapping("/api/project")
@Api(tags = "项目管理接口")
@ApiImplicitParams({
   @ApiImplicitParam(paramType = "header", name = "authorization", value = "操作人手机号码", dataType = "String", required = true, example = "185xxxxx"),
})
public class ProjectController extends BaseController {

    @Resource
    private ProjectService projectService;

    public ProjectController(HttpServletRequest request) {
        super(request);
    }

    /**
     * 创建项目
     */
    @PostMapping("/create")
    @ApiOperation("创建项目")
    public ResponseMessage<Boolean> createProject(@RequestBody @ApiParam("项目信息") ProjectEntity projectEntity) {
        projectEntity.setUserId(getUserId());
        return Result.success(projectService.createProject(projectEntity));
    }

    /**
     * 更新项目
     */
    @PostMapping("/update")
    @ApiOperation("更新项目")
    public ResponseMessage<Boolean> updateProject(@RequestBody @ApiParam("项目信息") ProjectEntity projectEntity) {
        return Result.success(projectService.updateProject(projectEntity));
    }

    /**
     * 删除项目
     */
    @DeleteMapping("/delete/{projectId}")
    @ApiOperation("删除项目")
    public ResponseMessage<Boolean> deleteProject(@PathVariable("projectId") @ApiParam("项目ID") Integer projectId) {
        return Result.success(projectService.deleteProject(projectId));
    }

    /**
     * 获取项目详情
     */
    @GetMapping("/get/{projectId}")
    @ApiOperation("获取项目详情")
    public ResponseMessage<ProjectEntity> getProject(@PathVariable("projectId") @ApiParam("项目ID") Integer projectId) {
        return Result.success(projectService.getProject(projectId));
    }

    /**
     * 获取用户的所有项目
     */
    @GetMapping("/list")
    @ApiOperation("获取用户的所有项目")
    public ResponseMessage<List<ProjectEntity>> getUserProjects() {
        Long userId = getUserId();
        return Result.success(projectService.getUserProjects(userId));
    }

    /**
     * 分页查询用户项目
     */
    @GetMapping("/page")
    @ApiOperation("分页查询用户项目")
    public ResponseMessage<Page<ProjectEntity>> getUserProjectsByPage(
            @RequestParam(defaultValue = "1") @ApiParam("页码") int page,
            @RequestParam(defaultValue = "10") @ApiParam("每页大小") int size,
            @RequestParam @ApiParam("用户ID") Integer userId,
            @RequestParam(required = false) @ApiParam("项目类型") String projectType,
            @RequestParam(required = false) @ApiParam("语言类型") String languageType,
            @RequestParam(required = false) @ApiParam("项目状态") String projectStatus,
            @RequestParam(required = false) @ApiParam("项目名称") String projectName) {
        return Result.success(projectService.getUserProjectsByPage(page, size, userId, 
                projectType, languageType, projectStatus, projectName));
    }
}
